
class_name CClassBagItemArray
extends EventDispatcher



class SClassBagItem extends CBagItemArray.SBagItem:
    pass

    #  SClassBagItem Geter Setter method # 


# ================================= array ==========================
var m_UUID
var m_nClassBagItemCount
var m_ClassBagItem

signal elem_update(array, old_value, new_value, index)
signal elem_add(array, old_value, new_value, index)
signal elem_del(array, old_value, new_value, index)

func _init( _uuid ):
    self.m_UUID = _uuid
    self.m_nClassBagItemCount = 0
    self.m_ClassBagItem = []
    var nMaxIndex = self.GetClassBagItemMaxCount()-1
    self.m_ClassBagItem.resize(nMaxIndex)
    for i in range(nMaxIndex):
        self.m_ClassBagItem[i] = SClassBagItem.new(self) 
        self.m_ClassBagItem[i].nIndex = i

func UnPack(_bufferStream):
    self.m_UUID = _bufferStream.ReadInt64()
    self.m_nClassBagItemCount = _bufferStream.ReadUint32()
    for i in range(self.m_nClassBagItemCount):
        var elem = SClassBagItem.new(self)
        elem.nIndex = i
        elem.UnPack(_bufferStream, false)
        self.m_ClassBagItem[i] = elem

func GetTableName():
    return 'ClassBagItem'

func GetUUID():
    return self.m_UUID

func GetClassBagItemCurNum():
    return self.m_nClassBagItemCount

func SetClassBagItemCurNum( nValue ):
    self.m_nClassBagItemCount = nValue

func SetCurNum(nValue):
    self.m_nClassBagItemCount = nValue

static func GetClassBagItemMaxCount():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM

func GetClassBagItemMaxNum():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM

func GetClassBagItem(_nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM:
        print("GetClassBagItemEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM")
        return null

    return self.m_ClassBagItem[_nIndex];

func SetClassBagItem(_Prop, _nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM:
        print("SetClassBagItemEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM")
        return null
    
    var old = self.m_ClassBagItem[_nIndex]
    var elem = SClassBagItem.new(self)
    elem.CopyFrom(_Prop)
    elem.nIndex = _nIndex
    self.m_ClassBagItem[_nIndex] = elem
    elem_update.emit(self, old, elem, _nIndex)
    return elem


func AddClassBagItem(_Prop, _nIndex):
    var nCurNum = self.m_nClassBagItemCount
    if _nIndex < 0 or _nIndex > nCurNum:
        _nIndex = nCurNum

    if not _Prop or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM:
        print("AddClassBagItemEle _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM");
        return null;

    var elem = SClassBagItem.new(self)
    elem.CopyFrom(_Prop)
    if _nIndex < nCurNum:
        for i in range(nCurNum-1, -1, -1):
            self.m_ClassBagItem[i] = self.m_ClassBagItem[i - 1]
            self.m_ClassBagItem[i].nIndex = i
    self.m_ClassBagItem[_nIndex] = elem
    self.m_nClassBagItemCount = nCurNum + 1
    elem_add.emit(self, null, elem, _nIndex)

func DelClassBagItem(_nIndex):
    if _nIndex < 0 or _nIndex >= self.m_nClassBagItemCount:
        return
    
    self.m_nClassBagItemCount = self.m_nClassBagItemCount - 1
    if _nIndex != self.m_nClassBagItemCount:
        self.m_ClassBagItem[_nIndex] = self.m_ClassBagItem[self.m_nClassBagItemCount]

    # var nLastDataID = self.m_ClassBagItem[self.m_nClassBagItemCount]
    var elem = self.m_ClassBagItem[_nIndex]
    for i in range(_nIndex + 1, self.m_nClassBagItemCount - 1):
        self.m_ClassBagItem[i - 1] = self.m_ClassBagItem[i]
        self.m_ClassBagItem[i - 1].nIndex = i - 1
    self.m_ClassBagItem[self.m_nClassBagItemCount - 1] = null
    elem_del.emit(self, elem, null, _nIndex)


func CopyFrom(_pOther):
    self.m_nClassBagItemCount = _pOther.m_nClassBagItemCount
    for i in range(_pOther.m_nClassBagItemCount):
        self.m_ClassBagItem[i] = SClassBagItem.new(self)
        self.m_ClassBagItem[i].CopyFrom(_pOther.m_ClassBagItem[i])

func UnPackField(_fieldId, _bufferStream, _varLen):
    var elemCount = self.m_nClassBagItemCount
    if _fieldId < 0 or _fieldId > TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM:
        # var szLog = string.format("CClassBagItemArray.UnPackField Error :\
        #  _fieldId(%d) < 0 or _fieldId(%d) > self.MaxClassBagItemEleCount()(%d)",
        #  _fieldId, _fieldId, TableCommonDefine.MaxRowElem.MAX_COUNT_CLASSBAGITEM)
        return false
    var elem = null
    var old = null
    if self.m_ClassBagItem[_fieldId] != null:
        elem = self.m_ClassBagItem[_fieldId]
        old = SClassBagItem.new( self )
        old.CopyFrom( elem )
    else:
        elem = SClassBagItem.new(self)
        self.m_ClassBagItem[_fieldId] = elem
        self.m_ClassBagItem[_fieldId].nIndex = _fieldId

    if elemCount <= _fieldId:
        self.m_nClassBagItemCount = _fieldId + 1

    elem.Update(_bufferStream, _fieldId )
    elem_update.emit(self, old, elem, _nIndex)
    return true

func AddField(_fieldId, _bufferStream):
    var elem = SClassBagItem.new(self)
    elem.UnPack(_bufferStream, true )
    elem.nIndex = _fieldId
    if null == self.m_ClassBagItem[_fieldId]:
        self.m_ClassBagItem[_fieldId] = elem
    else:
        var uuid = elem.m_UUID;
        for index in range(self.m_nClassBagItemCount, _fieldId+1, -1):
            self.m_ClassBagItem[index] = self.m_ClassBagItem[index- 1]
            self.m_ClassBagItem[index].m_UUID = uuid + index - _fieldId
            self.m_ClassBagItem[index].nIndex = index
        self.m_ClassBagItem[_fieldId] = elem;
        self.m_ClassBagItem[_fieldId].nIndex = _fieldId

    self.m_nClassBagItemCount = self.m_nClassBagItemCount + 1;
    elem_add.emit(self, null, elem, _fieldId)

func DelField(_fieldId ):
    if self.m_ClassBagItem[_fieldId] == null:
        return 
    
    self.m_nClassBagItemCount = self.m_nClassBagItemCount - 1
    var elem = self.m_ClassBagItem[_fieldId]
    if _fieldId != self.m_nClassBagItemCount:
        var uuid = self.m_ClassBagItem[_fieldId].m_UUID;
        self.m_ClassBagItem[_fieldId] = self.m_ClassBagItem[self.m_nClassBagItemCount]
        self.m_ClassBagItem[_fieldId].m_UUID = uuid
        self.m_ClassBagItem[_fieldId].nIndex = _fieldId

    self.m_ClassBagItem[self.m_nClassBagItemCount] = null
    elem_del.emit(self, elem, null, _fieldId)   

func Filter( funFilter ):
    var tblItems = []
    for i in range(self.m_nClassBagItemCount-1):
        if funFilter.call(self.m_ClassBagItem[i], i):
            tblItems.append(self.m_ClassBagItem[i])
    return tblItems

func QueryByMaskID(MaskID):
    var nCnt = self.m_nClassBagItemCount
    for i in range(nCnt-1):
        var curElem = self.m_ClassBagItem[i]
        if curElem != null :
            if curElem.m_MaskID == MaskID :
                return curElem
    return null

func QueryByItemUUID(ItemUUID):
    var nCnt = self.m_nClassBagItemCount
    for i in range(nCnt-1):
        var curElem = self.m_ClassBagItem[i]
        if curElem != null :
            if curElem.m_MaskID > 0 and curElem.m_ItemUUID == ItemUUID :
                return curElem
    return null

func QueryAllDataByItemID(ItemID):
    var nCnt = self.m_nClassBagItemCount
    var tblData = []
    for i in range(nCnt - 1):
        var curElem = self.m_ClassBagItem[i]
        if curElem != null:
            if curElem.m_MaskID > 0 and curElem.m_ItemID == ItemID :
                tblData.append(curElem)
    return tblData

